package com.health.userbackstage.web.control;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.health.userbackstage.pojo.CheckGroup;
import com.health.userbackstage.pojo.SetMeal;
import com.health.userbackstage.pojo.result.Result;
import com.health.userbackstage.service.CheckGroupService;
import com.health.userbackstage.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author ASUS
 * @Date 2023/8/22 19:53
 * @Version 1.0
 */
@RestController
@RequestMapping("/setmeal")
public class SetMealController {

    private String basePath = "D:/upload/";

    @Autowired
    private SetMealService sms;

    @Autowired
    private CheckGroupService cgs;

    @RequestMapping("/findpage")
    private Result findPage(@RequestBody SetMeal sm){
        IPage<SetMeal> page = new Page<>(sm.getCurrentPage(),sm.getPageSize());
        QueryWrapper qw = new QueryWrapper();
        if(!"".equals(sm.getQueryString())&&sm.getQueryString()!=null){
            qw.eq("code",sm.getQueryString());
            qw.or();
            qw.eq("name",sm.getQueryString());
            qw.or();
            qw.eq("helpcode",sm.getQueryString());
        }else {
            qw = null;
        }
        sms.page(page,qw);
        return Result.success(page);
    }

    @RequestMapping("/getcheckgroup")
    private Result getCheckGroup(){
        List<CheckGroup> arr = cgs.list();
        return Result.success(arr);
    }

    @RequestMapping("/upload")
    private Result upload(MultipartFile imgFile, HttpServletRequest req) throws IOException {
        String uploadFileName = imgFile.getOriginalFilename();
        String uuid = UUID.randomUUID().toString().replace("-","");
        int lastDotIdx = uploadFileName.lastIndexOf(".");
        String fileSuffix = uploadFileName.substring(lastDotIdx);
        String fileName = uuid+fileSuffix;
        File saveFile = new File(basePath + fileName);
        imgFile.transferTo(saveFile);

        return Result.success(fileName);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody SetMeal setMeal){
        String str = sms.add(setMeal);
        if(str.equals("成功")){
            return Result.success();
        }else {
            return Result.fail(str);
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody SetMeal setMeal){
        String str = sms.edit(setMeal);
        if(str.equals("成功")){
            return Result.success();
        }else {
            return Result.fail(str);
        }
    }

    @RequestMapping("/del")
    public Result del(@RequestBody SetMeal setMeal){
        sms.del(setMeal);
        return Result.success();
    }

    @RequestMapping("/getcheckgroupids")
    public Result getCheckGroupIds(Integer id){
        List<Integer> checkGroupIds = sms.getCheckGroupIds(id);
        return Result.success(checkGroupIds);
    }

    @RequestMapping("/getReport")
    public Result getReport(){
        return Result.success(sms.getSetMealReport());
    }

    @RequestMapping("/getAllReport")
    public Result getAllReport(){
        return Result.success(sms.getAllSetMealReport());
    }



    @RequestMapping("/download")
    public void download(String name, HttpServletResponse response){
        try {
            //输入流，通过输入流读取文件内容
            FileInputStream fileInputStream = new FileInputStream(new File(basePath + name));

            //输出流，通过输出流将文件写回浏览器
            ServletOutputStream outputStream = response.getOutputStream();

            response.setContentType("image/jpeg");

            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = fileInputStream.read(bytes)) != -1){
                outputStream.write(bytes,0,len);
                outputStream.flush();
            }

            //关闭资源
            outputStream.close();
            fileInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
